#include "communication_user.h"
#include "ui_communication_user.h"
#include <QMessageBox>
#include <QHostAddress>

communication_user::communication_user(int currentID, QString & role, QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    this->currentID = currentID;
    this->role = role;

    ui->setupUi(this);
    ui->port->setText("8899");
    ui->ip->setText("127.0.0.1");
    setWindowTitle("患者挂号专用");
     ui->connect->setDisabled(false);
    ui->disconnect->setEnabled(false);
    m_tcp=new QTcpSocket(this);

    connect(m_tcp,&QTcpSocket::readyRead,this,[=](){
        QByteArray data =m_tcp->readAll();
        ui->record ->append("医生say:"+data);
    });

    connect(m_tcp,&QTcpSocket::disconnected,this,[=]()
    {m_tcp->close();
     m_tcp->deleteLater();
     m_status->setPixmap(QPixmap(":/OIP-C (1).jpg").scaled(20,20));
     ui->record->append("沟通服务结束");
    });

    connect(m_tcp,&QTcpSocket::connected,this,[=](){
        m_status->setPixmap(QPixmap(":/OIP-C.jpg").scaled(20,20));
        ui->record->append("沟通服务开始");
        ui->connect->setDisabled(true);
        ui->disconnect->setEnabled(true);
    });


m_status=new QLabel;
m_status->setPixmap(QPixmap(":/OIP-C (1).jpg").scaled(20,20));
ui->statusbar->addWidget(new QLabel("连接状态:"));
ui ->statusbar->addWidget(m_status);
}

communication_user::~communication_user()
{
    delete ui;
}


void communication_user::on_sendMsg_clicked()
{
    QString msg=ui->msg ->toPlainText();
    m_tcp->write(msg.toUtf8());
    ui->record->append("患者say:"+msg);
}
;
void communication_user::on_connect_clicked()
{
    QString ip =ui->ip->text();
    unsigned short port =ui->port ->text().toUShort();
    m_tcp->connectToHost(QHostAddress(ip),port);
}

void communication_user::on_disconnect_clicked()
{
   m_tcp->close();
    ui->connect->setDisabled(false);
   ui->disconnect->setEnabled(false);
}
